package com.sunlands.qdcy.deskmate.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 提供MD5操作
 *
 * @author jiangzheng
 */
public final class MD5Util {

    private MD5Util() {
    }

    /**
     * 转换成十六进制
     *
     * @param b 二进制数组
     * @return
     * @author liufengyu
     * @date 2013-1-24
     */
    public static String byte2hex(byte[] b) { // 二行制转十六进制字符串
        if (b == null) {
            return "";
        }
        StringBuffer hs = new StringBuffer();
        String stmp = null;
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1) {
                hs.append("0");
            }
            hs.append(stmp);
        }
        return hs.toString();
    }

    /**
     * 计算MD5值
     *
     * @param src 字符串
     * @return
     * @author liufengyu
     * @date 2013-1-24
     */
    public static String MD5(String src) throws Exception {
        if (src == null) {
            return "";
        }
        byte[] result = null;
        try {
            MessageDigest alg = MessageDigest.getInstance("MD5");
            result = alg.digest(src.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            throw e;
        } catch (UnsupportedEncodingException e) {
            throw e;
        }
        return byte2hex(result);
    }

    /**
     * 计算MD5值
     *
     * @param src 二进制数组
     * @return
     * @author liufengyu
     * @date 2013-1-24
     */
    public static String MD5(byte[] src) throws Exception{
        if (src == null) {
            return "";
        }
        byte[] result = null;
        try {
            MessageDigest alg = MessageDigest.getInstance("MD5");
            result = alg.digest(src);
        } catch (NoSuchAlgorithmException e) {
            throw e;
        }
        return byte2hex(result);
    }

    /**
     * @param src 二进制数组
     * @return
     * @author liufengyu
     * @date 2013-1-24
     */
    public static byte[] MD5Bytes(byte[] src) throws Exception {
        byte[] result = null;
        if (src != null) {
            try {
                MessageDigest alg = MessageDigest.getInstance("MD5");
                result = alg.digest(src);
            } catch (NoSuchAlgorithmException e) {
                throw e;
            }
        }
        return result;
    }

    public final static String MD5Normal(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param args 参数
     * @author liufengyu
     * @date 2013-1-24
     */
//    public static void main(String[] args) {
//        //System.out.println(MD5("2221083" + "20111101" + "pagehis"));
//    }
}